<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/jexo/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/jexo/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/jexo/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/jexo/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/jexo/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/jexo/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/jexo/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Android,RxJava,源码解析," />










<meta name="description" content="严格来说，create应不算操作符，但是第一篇文章我还是希望能先以最简单的入门。这样再去学习接下来的操作符的话会更加简单，由浅入深。">
<meta name="keywords" content="Android,RxJava,源码解析">
<meta property="og:type" content="article">
<meta property="og:title" content="我是最简单的操作符-----Create">
<meta property="og:url" content="http://ja_project.gitee.io/jexo/2018/04/18/我是最简单的操作符-Create/index.html">
<meta property="og:site_name" content="Ling Hu Blog">
<meta property="og:description" content="严格来说，create应不算操作符，但是第一篇文章我还是希望能先以最简单的入门。这样再去学习接下来的操作符的话会更加简单，由浅入深。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-04-18T13:46:31.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="我是最简单的操作符-----Create">
<meta name="twitter:description" content="严格来说，create应不算操作符，但是第一篇文章我还是希望能先以最简单的入门。这样再去学习接下来的操作符的话会更加简单，由浅入深。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/jexo/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://ja_project.gitee.io/jexo/2018/04/18/我是最简单的操作符-Create/"/>





  <title>我是最简单的操作符-----Create | Ling Hu Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?abc8796c232e2b898d516245257842b4";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/jexo/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Ling Hu Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/jexo/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/jexo/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/jexo/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/jexo/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/jexo/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://ja_project.gitee.io/jexo/jexo/2018/04/18/我是最简单的操作符-Create/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Ling Hu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/jexo/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Ling Hu Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">我是最简单的操作符-----Create</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-04-18T21:45:52+08:00">
                2018-04-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/jexo/categories/RxJava操作符源码解析/" itemprop="url" rel="index">
                    <span itemprop="name">RxJava操作符源码解析</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/jexo/2018/04/18/我是最简单的操作符-Create/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/jexo/2018/04/18/我是最简单的操作符-Create/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>严格来说，create应不算操作符，但是第一篇文章我还是希望能先以最简单的入门。这样再去学习接下来的操作符的话会更加简单，由浅入深。</p>
</blockquote>
<a id="more"></a>
<h6 id="来段小代码"><a href="#来段小代码" class="headerlink" title="来段小代码"></a>来段小代码</h6><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ObservaleActivity</span> : <span class="type">AppCompatActivity</span></span>() &#123;</span><br><span class="line">    <span class="keyword">var</span> sub: Subscription? = <span class="literal">null</span></span><br><span class="line">    <span class="keyword">var</span> observable: Observable&lt;String&gt;? = <span class="literal">null</span></span><br><span class="line">    <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">onCreate</span><span class="params">(savedInstanceState: <span class="type">Bundle</span>?)</span></span> &#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState)</span><br><span class="line">        setContentView(R.layout.activity_common)</span><br><span class="line">        btSub.setOnClickListener(&#123;</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * 暂时都不考虑 onComplete onError 只做onNext的处理，由浅入深</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            observable?.subscribe(&#123; msg -&gt;</span><br><span class="line">                tvContent.text = tvContent.text.toString() + <span class="string">"\n"</span> + msg</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 最简单的使用方式</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        observable = Observable.create(<span class="keyword">object</span> : Observable.OnSubscribe&lt;String&gt; &#123;</span><br><span class="line">            <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">call</span><span class="params">(t: <span class="type">Subscriber</span>&lt;<span class="type">in</span> <span class="type">String</span>&gt;)</span></span> &#123;</span><br><span class="line">                t.onNext(<span class="string">"Test1"</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//这里需要去解除注册,防止内存泄漏</span></span><br><span class="line">    <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">onDestroy</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">super</span>.onDestroy()</span><br><span class="line">        sub?.unsubscribe()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里直接先贴出整个<code>Activity</code>的代码，给大家一个印象，在以后的话就不再直接贴全代码，而是贴关键代码。</p>
<blockquote>
<p>注意：一般来说，是需要在onDestroy的时候去unsubscribe，防止内存泄漏</p>
</blockquote>
<hr>
<h6 id="先了解重要的接口和类"><a href="#先了解重要的接口和类" class="headerlink" title="先了解重要的接口和类"></a>先了解重要的接口和类</h6><p>在阅读源码的时候，我get到了几个重要的方法</p>
<p><strong>1. 不需要每行都去搞懂</strong><br><strong>2. 抓住最重要的类和接口去搞懂</strong></p>
<p>这里比较重要的接口和类分别是:</p>
<table>
<thead>
<tr>
<th style="text-align:center">接口或类名</th>
<th style="text-align:center"></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>OnSubscribe</code></td>
<td style="text-align:center">实现了Action接口，只有一个call方法，在subscribe时候才会去调用</td>
</tr>
<tr>
<td style="text-align:center"><code>Subscriber</code></td>
<td style="text-align:center">调用subscribe所传的参数,里面是onNext,onComplete,onError等方法</td>
</tr>
<tr>
<td style="text-align:center"><code>Observable</code></td>
<td style="text-align:center">所有的操作符都在这个类里面，我认为最重要的其实是OnSubscribe和Subscriber,而Observable其实是作为一个桥梁来链接这2者</td>
</tr>
<tr>
<td style="text-align:center"><code>Subscription</code></td>
<td style="text-align:center">这个是调用subscribe方法的返回值，用来取消这次订阅</td>
</tr>
</tbody>
</table>
<p>我阅读RxJavau 源码的时候，最让我头疼的一个地方其实就是它的命名，很多名字起的都起的差不多，所以搞得很乱。</p>
<p>这里我专门提出这4个接口或类，只要能把这几个理清楚，其实就差不多。下面我们深入源码去理解。</p>
<h6 id="看看源代码"><a href="#看看源代码" class="headerlink" title="看看源代码"></a>看看源代码</h6><blockquote>
<p>Observable.create方法</p>
</blockquote>
<p>Observable<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">Observable&lt;T&gt; <span class="title">create</span><span class="params">(OnSubscribe&lt;T&gt; f)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Observable&lt;T&gt;(RxJavaHooks.onCreate(f));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>RxJavaHooks<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; Observable.<span class="function">OnSubscribe&lt;T&gt; <span class="title">onCreate</span><span class="params">(Observable.OnSubscribe&lt;T&gt; onSubscribe)</span> </span>&#123;</span><br><span class="line">        Func1&lt;Observable.OnSubscribe, Observable.OnSubscribe&gt; f = onObservableCreate;</span><br><span class="line">        <span class="keyword">if</span> (f != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> f.call(onSubscribe);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> onSubscribe;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p>这2段代码涉及到了刚才提到的<code>Observable</code>,<code>OnSubscribe</code>。<br><code>Observable.create(OnSubscribe)</code>其实只是new了一个Observable然后把OnSubscribe作为参数传入，这里的RxJavaHooks大家可以先忽略，这个主要是做全局处理使用的，如果没有全局设置的话，<code>onObservableCreate = null</code>,会直接在第二个方法直接返回你传入的OnSubscribe参数。</p>
<blockquote>
<p>Observable.subscribe方法</p>
</blockquote>
<p>Observable<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">public</span> <span class="keyword">final</span> Subscription subscribe(<span class="keyword">final</span> Action1&lt;? <span class="keyword">super</span> T&gt; onNext) &#123;</span><br><span class="line">        <span class="keyword">if</span> (onNext == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> new IllegalArgumentException(<span class="string">"onNext can not be null"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Action1&lt;Throwable&gt; onError = InternalObservableUtils.ERROR_NOT_IMPLEMENTED;</span><br><span class="line">        Action0 onCompleted = Actions.empty();</span><br><span class="line">        <span class="keyword">return</span> subscribe(new ActionSubscriber&lt;T&gt;(onNext, onError, onCompleted));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">final</span> Subscription subscribe(Subscriber&lt;? <span class="keyword">super</span> T&gt; subscriber) &#123;</span><br><span class="line">        <span class="keyword">return</span> Observable.subscribe(subscriber, <span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">static &lt;T&gt; Subscription subscribe(Subscriber&lt;? <span class="keyword">super</span> T&gt; subscriber, Observable&lt;T&gt; observable) &#123;</span><br><span class="line">        ...</span><br><span class="line">        subscriber.onStart();</span><br><span class="line">        <span class="keyword">if</span> (!(subscriber instanceof SafeSubscriber)) &#123;</span><br><span class="line">            subscriber = new SafeSubscriber&lt;T&gt;(subscriber);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            RxJavaHooks.onObservableStart(observable, observable.onSubscribe).call(subscriber);</span><br><span class="line">            <span class="keyword">return</span> RxJavaHooks.onObservableReturn(subscriber);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Throwable e) &#123;</span><br><span class="line">            ...</span><br><span class="line">            <span class="keyword">return</span> Subscriptions.unsubscribed();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p>subscribe重载方法比较多，我把我们demo中涉及到的3个重载列出。<br>在demo中我们使用的是直接传入了一个<code>Action</code>对象处理了onNext的事件，在源码中，Observable会把我们的<code>Action</code>包装成一个<code>Subscriber</code>对象，添加上<code>onError</code>,<code>onComplete</code>等方法的默认实现。</p>
<p>最终调用的其实是<code>subscribe</code>的第三个重载方法。<br>首先判断下参数<code>Subscriber</code>对象是否是<code>SafeSubscriber</code>对象的子类，如果不是，那么就重新new一个<code>SafeSubscriber</code>替换掉当前<code>Observable</code>对象的<code>subscriber</code>变量。</p>
<blockquote>
<p>注意：这里的<code>SafeSubscriber</code>大家可以先忽略，我们先看完完整的流程，在最后再为大家来看看<code>SafeSubscriber</code></p>
</blockquote>
<p>最重要的代码<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">RxJavaHooks.onObservableStart(observable, observable.onSubscribe).call(subscriber);</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>RxJavaHooks<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> static &lt;T&gt; Observable.OnSubscribe&lt;T&gt; onObservableStart(Observable&lt;T&gt; instance, Observable.OnSubscribe&lt;T&gt; onSubscribe) &#123;</span><br><span class="line">       Func2&lt;Observable, Observable.OnSubscribe, Observable.OnSubscribe&gt; f = onObservableStart;</span><br><span class="line">       <span class="keyword">if</span> (f != <span class="literal">null</span>) &#123;</span><br><span class="line">           <span class="keyword">return</span> f.call(instance, onSubscribe);</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">return</span> onSubscribe;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<p>这里又出现了<code>RxJavaHooks</code>，大家可以直接忽略看最后返回的参数是什么，直接返回的是传入的参数<code>OnSubscribe</code>对象。这里的<code>RxJavaHooks</code>也是用来做全局处理的作用，所以我们可以先忽略，简单的来看整个流程。</p>
<p>所以上面所提到的最终的代码，其实可以简化为<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">observable.onSubscribe.call(subscriber)</span><br></pre></td></tr></table></figure></p>
<p>大家可以看到，其实这就是最关键的代码，也可以看出 <code>Observable</code>这个对象的作用，其实就是用来链接<code>OnSubscribe</code>和<code>Subscriber</code>对象。</p>
<p>那么我们重新来看看<code>Activity</code>中的关键的代码<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">observable?.subscribe(&#123; msg -&gt;</span><br><span class="line">                tvContent.text = tvContent.text.toString() + <span class="string">"\n"</span> + msg</span><br><span class="line">            &#125;)</span><br><span class="line">...</span><br><span class="line">observable = Observable.create(<span class="keyword">object</span> : Observable.OnSubscribe&lt;String&gt; &#123;</span><br><span class="line">            <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">call</span><span class="params">(t: <span class="type">Subscriber</span>&lt;<span class="type">in</span> <span class="type">String</span>&gt;)</span></span> &#123;</span><br><span class="line">                t.onNext(<span class="string">"Test1"</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>应该对整个流程有一个简单的认识了。</p>
<hr>
<h6 id="附加知识点"><a href="#附加知识点" class="headerlink" title="附加知识点"></a>附加知识点</h6><p>SafeSubscriber<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> void onCompleted() &#123;</span><br><span class="line">       <span class="keyword">if</span> (!done) &#123;</span><br><span class="line">           done = <span class="literal">true</span>;</span><br><span class="line">           <span class="keyword">try</span> &#123;</span><br><span class="line">               actual.onCompleted();</span><br><span class="line">           &#125; <span class="keyword">catch</span> (Throwable e) &#123;</span><br><span class="line">          ...</span><br><span class="line">         &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="meta">@Override</span></span><br><span class="line">   <span class="keyword">public</span> void onError(Throwable e) &#123;</span><br><span class="line">       <span class="keyword">if</span> (!done) &#123;</span><br><span class="line">           done = <span class="literal">true</span>;</span><br><span class="line">           _onError(e);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   <span class="meta">@Override</span></span><br><span class="line">   <span class="keyword">public</span> void onNext(T t) &#123;</span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           <span class="keyword">if</span> (!done) &#123;</span><br><span class="line">                actual.onNext(t);</span><br><span class="line">             &#125;</span><br><span class="line">       ...</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>主要作用: 对原有的<code>Subscriber</code>对象包装一下。前面刚学RxJava的时候，看到有些文章说，<code>onError</code>和<code>onComplete</code>只有一个会运行，其实从这里可以看出。</p>
</blockquote>
<hr>
<h6 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h6><p><code>RxJava</code>中核心的设计模式我认为是<code>装饰者模式</code>，所以在阅读源码的时候也非常的累，因为<code>RxJava</code>的链式结构，大家可以想象下如果4，5个操作符链式书写，然后一步步跟入源码会很难受的，完全不知道当前是哪个<code>OnSubscribe</code>和<code>Subscriber</code>。</p>
<p>当然只要我们能搞清楚1层结构的流程，那么后面的3,4层结构也能理解，只是稍微麻烦点而已。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>Donate comment here</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/jexo/images/wechatpay.png" alt="Ling Hu 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/jexo/images/alipay.png" alt="Ling Hu 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/jexo/tags/Android/" rel="tag"># Android</a>
          
            <a href="/jexo/tags/RxJava/" rel="tag"># RxJava</a>
          
            <a href="/jexo/tags/源码解析/" rel="tag"># 源码解析</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/jexo/2018/04/18/RxJava操作符源码解析/" rel="next" title="RxJava操作符源码解析">
                <i class="fa fa-chevron-left"></i> RxJava操作符源码解析
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/jexo/2018/04/18/看看谁最快的操作符-Amb/" rel="prev" title="看看谁最快的操作符-----Amb">
                看看谁最快的操作符-----Amb <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/jexo/images/avatar.jpg"
                alt="Ling Hu" />
            
              <p class="site-author-name" itemprop="name">Ling Hu</p>
              <p class="site-description motion-element" itemprop="description">Android,源码解析,每天进步一点点</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/jexo/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/jexo/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/jexo/tags/index.html">
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/javalong" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-6"><a class="nav-link" href="#来段小代码"><span class="nav-number">1.</span> <span class="nav-text">来段小代码</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#先了解重要的接口和类"><span class="nav-number">2.</span> <span class="nav-text">先了解重要的接口和类</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#看看源代码"><span class="nav-number">3.</span> <span class="nav-text">看看源代码</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#附加知识点"><span class="nav-number">4.</span> <span class="nav-text">附加知识点</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#总结"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ling Hu</span>

  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/jexo/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/jexo/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/jexo/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/jexo/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/jexo/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/jexo/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  







<!-- LOCAL: You can save these files to your site and update links -->
    
        
        <link rel="stylesheet" href="https://aimingoo.github.io/gitmint/style/default.css">
        <script src="https://aimingoo.github.io/gitmint/dist/gitmint.browser.js"></script>
    
<!-- END LOCAL -->

    
      <style>
        a.gitment-editor-footer-tip { display: none; }
        .gitment-container.gitment-footer-container { display: none; }
      </style>
    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitmint({
            id: getId(),
            owner: 'javalong',
            repo: 'RxJava-Operator',
            
            lang: "zh-Hans" || navigator.language || navigator.systemLanguage || navigator.userLanguage,
            
            oauth: {
            
            
                client_secret: '06b042e57b8420f5002a6407348dde078bcde761',
            
                client_id: '1b5481d6512b2f28d7a2'
            }});
        gitment.render('gitment-container');
      }

      function getId(){
            if( window.location.pathname.length>=50){
                return window.location.pathname.substr(0,50)
            }else{
                return window.location.pathname
            }
      }

      
      renderGitment();
      
      </script>
    







  





  

  

  

  
  

  

  

  

</body>
</html>
